package com.panfeng.xcloud.common.security.exceptions;

import com.panfeng.xcloud.common.core.enums.ResponseStatusEnum;
import com.panfeng.xcloud.common.core.exceptions.BaseBizException;
import com.panfeng.xcloud.common.core.web.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * controller异常处理器
 *
 * @author xiaobo
 * @version 1.0
 * @since 2017-04-10
 */
@ControllerAdvice
@Slf4j
public class ControllerExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResponseVO<?> processValidationError(Exception e) {
        log.error(">>>Exception异常={}", e.getMessage(), e);
        ResponseVO responseVO = new ResponseVO();
        responseVO.setCode(ResponseStatusEnum.ERROR.getCode());
        responseVO.setMessage(ResponseStatusEnum.ERROR.getMessage());
        e.printStackTrace();
        return responseVO;
    }

    @ExceptionHandler(InternalAuthenticationServiceException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResponseVO<?> internalAuthenticationServiceException(InternalAuthenticationServiceException e) {
        log.error(">>> InternalAuthenticationServiceException异常={}", e.getMessage(), e);
        ResponseVO responseVO = new ResponseVO();
        responseVO.setCode(ResponseStatusEnum.ERROR.getCode());
        responseVO.setMessage(e.getMessage());
        return responseVO;
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResponseVO<?> illegalArgumentException(IllegalArgumentException e) {
        log.error(">>>IllegalArgumentException参数非法异常={}", e.getMessage(), e);
        ResponseVO responseVO = new ResponseVO();
        responseVO.setCode(ResponseStatusEnum.PARAMETER_ERROR.getCode());
        responseVO.setMessage(ResponseStatusEnum.PARAMETER_ERROR.getMessage());
        return responseVO;
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResponseVO<?> methodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(">>>MethodArgumentNotValidException参数非法异常={}", e.getMessage(), e);
        ResponseVO responseVO = new ResponseVO();
        responseVO.setCode(ResponseStatusEnum.PARAMETER_ERROR.getCode());
        responseVO.setMessage(ResponseStatusEnum.PARAMETER_ERROR.getMessage());
        return responseVO;
    }

    @ExceptionHandler(BaseBizException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResponseVO<?> businessException(BaseBizException e) {
        log.error(">>>BaseBizException业务异常={}", e.getMsg(), e);
        ResponseVO responseVO = new ResponseVO();
        responseVO.setCode(e.getErrorCode());
        responseVO.setMessage(e.getMsg());
        return responseVO;
    }

    @ExceptionHandler(AccessDeniedException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResponseVO<?> accessDeniedException(AccessDeniedException e) {
        log.error(">>>AccessDeniedException异常={}", e.getMessage(), e);
        ResponseVO responseVO = new ResponseVO();
        responseVO.setCode(ResponseStatusEnum.UNAUTHORIZED.getCode());
        responseVO.setMessage(ResponseStatusEnum.UNAUTHORIZED.getMessage());
        return responseVO;
    }
}
